//
//  ZFLoginHttpRequest.m
//  HappilyLoan
//
//  Created by zainishenbian on 2017/3/31.
//  Copyright © 2017年 zongqingxiangqian. All rights reserved.
//

#import "ZFLoginHttpRequest.h"
#import "MD5Util.h"
#import "ZFHeader.h"

@implementation ZFLoginHttpRequest

//获取短信验证码
+ (void)requestGetSmsCodePhone:(NSString *)phone withCodeType:(NSString *)codeType sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    NSDictionary *bodyParameter = @{@"code_type":codeType,@"phone":phone};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/user/sendSmsCodeForJSD",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        successBlock(responseObject);
        NSLog(@"%@-----%@",responseObject,responseObject[@"msg"]);
    } failure:^(NSError *error) {
        failureBlock(1);
    }];
}
//请求注册
+ (void)requestRegisterPhone:(NSString *)phone smsCode:(NSString *)smsCode pwd:(NSString *)pwd sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    NSDictionary *bodyParameter = @{@"login_pwd":pwd,@"phone":phone,@"sms_code":smsCode};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/user/registerUser",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
            }
            [userDefaults synchronize];
        }
        successBlock(responseObject);
    } failure:^(NSError *error) {
        failureBlock(1);
    }];
}
//请求账号密码登录
+ (void)requestLoginPhone:(NSString *)phone pwd:(NSString *)pwd sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock;
{
    NSDictionary *bodyParameter = @{@"login_pwd":pwd,@"phone":phone};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/user/loginByPassword",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:responseObject[@"data"][@"user_id"] forKey:@"userid"];
            [userDefaults setObject:responseObject[@"data"][@"info_progress"] forKey:@"info_progress"];
            [userDefaults setObject:responseObject[@"data"][@"phone"] forKey:@"phone"];
            if (responseObject[@"data"][@"user_name"] != [NSNull null]) {
                [userDefaults setObject:responseObject[@"data"][@"user_name"] forKey:@"user_name"];
            }
            [userDefaults synchronize];
        }
        successBlock(responseObject);
        NSLog(@"%@-----%@",responseObject[@"msg"],responseObject);
    } failure:^(NSError *error) {
        failureBlock(1);
    }];
}
//请求短信验证码登录
+ (void)requestSmsLoginPhone:(NSString *)phone smsCode:(NSString *)smsCode sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
//    NSString *bodyStr = [NSString stringWithFormat:@"phone=%@&sms_code=%@",phone,smsCode];
    
    NSDictionary *bodyParameter = @{@"phone":phone,@"sms_code":smsCode};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/user/loginBySmsCode",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        successBlock(responseObject);
        NSLog(@"%@-----%@",responseObject,responseObject[@"msg"]);
    } failure:^(NSError *error) {
        failureBlock(1);
    }];
}
//修改密码
+ (void)requestFixPwdPhone:(NSString *)phone smsCode:(NSString *)smsCode pwd:(NSString *)pwd sucessBlock:(void(^)(NSDictionary *dict))successBlock failureBlock:(void(^)(NSInteger failSign))failureBlock
{
    NSDictionary *bodyParameter = @{@"new_login_pwd":pwd,@"phone":phone,@"sms_code":smsCode};
    NSString *bodyStr = [ZFHttpTool sortBodyStr:bodyParameter];

    [ZFHttpTool postWithURL:[NSString stringWithFormat:@"%@/user/modifyLoginPwd",SERVERIP] params:bodyParameter bodyStr:bodyStr success:^(id responseObject) {
        
        NSLog(@"%@-----%@",responseObject,responseObject[@"msg"]);
        successBlock(responseObject);
    } failure:^(NSError *error) {
        failureBlock(1);
    }];
}
@end
